//
//  SNHotProductCell.m
//  app
//
//  Created by 王少宁 on 15/8/20.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNHotProductCell.h"

@interface SNHotProductCell ()
@property (nonatomic, retain) UIImageView * imgView;
@property (nonatomic, retain) UILabel * titleLabel;
/** 打折*/
@property (nonatomic, retain) UILabel * promotionLabel;
@property (nonatomic, retain) UILabel * priceLabel;
/** 原价*/
@property (nonatomic, retain) UILabel * originallyPriceLabel;
/** 货到付款等等优惠的View : 可能不止一个,也可能没有*/
@property (nonatomic, retain) UIView * activityView;
/** 商家名称*/
@property (nonatomic, retain) UILabel * shopNameLabel;

@property (nonatomic, strong) UIButton * mainBtn;

@end

@implementation SNHotProductCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier shopBaseModel:(SNShopBaseModel *)model row:(NSInteger)row
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier shopBaseModel:model]) {
        
        SNShopHotProductModel * hotProductModel = (SNShopHotProductModel *)model;
        
        
        DLog(@"cell Nei Row ____>>>%d",row);
        
        if (hotProductModel.hotProductArray.count) {
            [self createCellWithModel:hotProductModel row:row];
        }
        
    }
    return self;
}

- (void)createCellWithModel:(SNShopHotProductModel *)hotProductModel row:(NSInteger)row
{
    
//    DLog(@"hot__row == >>>>>%d",row);
    CGFloat imgViewH = SCREEN_WIDTH * 190/720 - 20;
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, imgViewH, imgViewH)];
//    _imgView.layer.borderColor = [XCommon hexStringToColor:kColor_gray_line].CGColor;
//    _imgView.layer.borderWidth = UP(1);
    _imgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_imgView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 0;
    _titleLabel.textColor = [XCommon hexStringToColor:@"#4e4e4e"];
    _titleLabel.font = JKAppGlobleFontSmallTagText;
    [self.contentView addSubview:_titleLabel];
    
    self.promotionLabel = CREATE_WITH_FRAME_OF(UILabel, [_imgView.ufo fx:1.0 ox:13 fy:1.0 oy:-32],
                                                 [self.contentView.ubo ow:40 oh:15]);
    _promotionLabel.textColor = [UIColor whiteColor];
    _promotionLabel.backgroundColor = [UIColor redColor];
    _promotionLabel.textAlignment = NSTextAlignmentCenter;
    _promotionLabel.font = JKAppGlobleFontMinTagText;
//    _promotionLabel.layer.cornerRadius = 2.5;
//    _promotionLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:_promotionLabel];
    
    _priceLabel = CREATE_WITH_FRAME_OF(UILabel, [_promotionLabel.ufo fx:1.0 ox:6 fy:0],
                                       [_promotionLabel.ubo ow:100 fh:1.0]);
    _priceLabel.font = JKAppGlobleFontSmallTagText;
    _priceLabel.textColor = [UIColor redColor];
    [self.contentView addSubview:_priceLabel];
    
    self.originallyPriceLabel = CREATE_WITH_FRAME_OF(UILabel, [_promotionLabel.ufo fx:0 fy:1.0 oy:2],
                                                 [_promotionLabel.ubo ow:100 fh:1.0]);
    _originallyPriceLabel.font = JKAppGlobleFontMinTagText;
    _originallyPriceLabel.textColor = [XCommon hexStringToColor:@"#888888"];
    [self.contentView addSubview:_originallyPriceLabel];
    
    _activityView = [[UIView alloc] init];
    [self.contentView addSubview:_activityView];
    
    _shopNameLabel = [[UILabel alloc] init];
    _shopNameLabel.textColor = [XCommon hexStringToColor:@"#a0a0a0"];
    _shopNameLabel.font = JKAppGlobleFontMinTagText;
    _shopNameLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_shopNameLabel];
    
    self.mainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.mainBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.mainBtn];
}

- (void)cellWithModel:(SNShopHotProductSubModel *)model Model:(SNShopHotProductModel *)hotProductModel Row:(NSInteger)row
{
    [_imgView sd_setImageWithURL:[NSURL URLWithString:model.pic]];
    
    CGFloat titleLabelW = SCREEN_WIDTH - 15 - _imgView.ufo.xx - 13;
    CGFloat textW = [XCommon widthForString:model.product_title fontSize:13];
    CGFloat titleLabelH = [XCommon heightForString:@"一行" fontSize:13 andWidth:titleLabelW];
    if (textW <= titleLabelW) {
        _titleLabel.frame = CGRectOf([_imgView.ufo fx:1.0 ox:13 fy:0],
                                     [self.contentView.ufo ow:titleLabelW oh:titleLabelH]);
    } else {
        _titleLabel.frame = CGRectOf([_imgView.ufo fx:1.0 ox:13 fy:0],
                                     [self.contentView.ufo ow:titleLabelW oh:titleLabelH * 2]);
    }
    _titleLabel.text = model.product_title;
    
    _promotionLabel.text = [NSString stringWithFormat:@"%.1f 折",model.sale_price.floatValue * 10/model.tag_price.floatValue];
    _priceLabel.text = [NSString stringWithFormat:@"￥%@",model.sale_price];
    
    self.originallyPriceLabel.text = [NSString stringWithFormat:@"￥%@",model.tag_price];
    [self.originallyPriceLabel sizeToFit];
    self.originallyPriceLabel.frame = CGRectOf([_promotionLabel.ufo fx:0 fy:1.0 oy:2],
                                                     [_promotionLabel.ubo ow:self.originallyPriceLabel.ubo.width fh:1.0]);

    UILabel * lineLabel = CREATE_WITH_FRAME_OF(UILabel, [_originallyPriceLabel.ubo fx:0 fy:0.5 py:1],
                                               [_originallyPriceLabel.ubo fw:1.0 ph:1]);
    lineLabel.layer.borderColor = [XCommon hexStringToColor:@"#888888"].CGColor;
    lineLabel.layer.borderWidth = UP(1);
    [_originallyPriceLabel addSubview:lineLabel];
    
    _activityView.frame = CGRectOf(CGPointMake(_originallyPriceLabel.ufo.xx+5, _originallyPriceLabel.ufo.y),
                                         [_originallyPriceLabel.ubo ow:80 fh:1.0]);
    if (!_activityView.subviews.count && model.promotionArray.count) {
        for (int i = 0; i < model.promotionArray.count; i++) {
            UILabel * activityLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            SNShopPromotionModel * promotionModel = model.promotionArray[i];
            activityLabel.font = [UIFont systemFontOfSize:10.0f];
            activityLabel.backgroundColor = [UIColor lightGrayColor];
            activityLabel.textColor = [UIColor whiteColor];
//            activityLabel.layer.cornerRadius = 2.5;
//            activityLabel.layer.masksToBounds = YES;
            activityLabel.text = promotionModel.name;
            [activityLabel sizeToFit];
            [_activityView addSubview:activityLabel];
        }
    }
    
    NSArray * activityLabelsArray = _activityView.subviews;
    __block UILabel * label;
    [activityLabelsArray enumerateObjectsUsingBlock:^(UILabel * activityLabel, NSUInteger idx, BOOL *stop) {
        
        if (idx == 0) {
            activityLabel.frame = CGRectMake(0, 0, activityLabel.ubo.width, _activityView.ubo.height);
        } else {
            activityLabel.frame = CGRectMake(label.ufo.xx + 5, 0, activityLabel.ubo.width, _activityView.ubo.height);
        }
        label = activityLabel;
    }];
    
    _shopNameLabel.text = model.shop_name;
    [_shopNameLabel sizeToFit];
    CGFloat shopNameLabelW = _shopNameLabel.ubo.width + 5;
    _shopNameLabel.frame = CGRectOf([self.ufo fx:0 ox:SCREEN_WIDTH-shopNameLabelW-10 oy:_activityView.ufo.y],
                                    [_originallyPriceLabel.ubo ow:shopNameLabelW fh:1.0]);
    
    
    self.mainBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, hotProductModel.cellHeight);
    self.mainBtn.tag = row + 1;
    
    
}

- (void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(itemClickWithShopBaseModel:row:index:)]) {
        [self.delegate itemClickWithShopBaseModel:self.shopBaseModel row:0 index:btn.tag - 1];
    }
}


- (void)drawRect:(CGRect)rect {
//    [self drawSeperatorLineLeft:10 right:10];
}

@end
